using FSM.Dal;
using FSM.IDal;
using FSM.IBll;
using FSM.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using Util.Excel;
using System.Linq;

namespace FSM.Bll
{
    public class TbFilePredeleteBll : ITbFilePredeleteBll
    {
        #region 模块 
        private readonly ITbFilePredeleteDal _dal;
        private readonly ITbFileRestoreDal _fileDal;
        private readonly ITbFileLogBll _fileLogBll;
        //private readonly IExcelAssistant _excel; 
        public TbFilePredeleteBll(ITbFilePredeleteDal dal, ITbFileRestoreDal fileDal, ITbFileLogBll fileLogBll)
        {
            _dal = dal;
            _fileDal = fileDal;
            _fileLogBll = fileLogBll;
            //_excel = excel; 
        }
        #endregion

        #region 查询 

        /// <summary> 
        /// 根据键值查询单个信息 
        /// </summary> 
        /// <param name="id">键值</param> 
        /// <returns></returns> 
        public tb_file_predelete Get(int? id)
        {
            return _dal.GetById(id);
        }

        /// <summary> 
        /// 分页查询批量信息 
        /// </summary> 
        /// <param name="upid">上级键值</param> 
        /// <param name="name">名称</param> 
        /// <param name="del_start_time">删除开始时间</param>
        /// <param name="del_end_time">删除结束时间</param>
        /// <param name="isTop">是否只查询顶级删除文件/文件夹（非级联删除的）</param>
        /// <param name="orderName">排序名称</param>
        /// <param name="orderType">排序类型</param>
        /// <param name="page">页码</param> 
        /// <param name="page_size">每页显示的条数</param> 
        /// <returns></returns> 
        public Tuple<List<tb_file_predelete>, int> ListPagin(int? upid, string name, DateTime? del_start_time, DateTime? del_end_time, bool isTop, string orderName, int? orderType, int page, int page_size)
        {
            ISugarQueryable<tb_file_predelete> queryable = _dal.CreateQueryable();
            queryable = queryable.Where(p => p.can_restroe == 1);
            queryable = queryable.WhereIF(upid != null, p => p.upid == upid);
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(name), p => p.name.Contains(name));
            queryable = queryable.WhereIF(del_start_time != null, p => p.delete_date >= del_start_time.Value);
            queryable = queryable.WhereIF(del_end_time != null, p => p.delete_date <= del_end_time.Value.AddDays(1).Date);
            queryable = queryable.WhereIF(isTop, p => p.delete_id == null || p.delete_id == p.id);
            if (!string.IsNullOrEmpty(orderName) && orderType != null)
            {
                OrderByType obt = orderType.Value == 0 ? OrderByType.Asc : OrderByType.Desc;
                switch (orderName)
                {
                    case "name":
                        queryable = queryable.OrderBy(p => p.name, obt);
                        break;
                    case "size":
                        queryable = queryable.OrderBy(p => p.size, obt);
                        break;
                    case "date":
                        queryable = queryable.OrderBy(p => p.edit_date, obt);
                        break;
                    default:
                        queryable = queryable.OrderBy(p => p.id, OrderByType.Asc);
                        break;
                }
            }
            else
            {
                queryable = queryable.OrderBy(p => p.id, OrderByType.Asc);
            }

            PageModel pm = new PageModel
            {
                PageIndex = page,
                PageSize = page_size
            };
            Tuple<List<tb_file_predelete>, int> result = _dal.GetPageListAndTotal(queryable, pm);
            return result;
        }

        /// <summary> 
        /// 分页查询批量信息 
        /// </summary> 
        /// <param name="upid">上级键值</param> 
        /// <param name="name">名称</param> 
        /// <param name="del_start_time">删除开始时间</param>
        /// <param name="del_end_time">删除结束时间</param>
        /// <param name="delete_id">删除的主ID</param>
        /// <param name="isTop">是否只查询顶级删除文件/文件夹（非级联删除的）</param>
        /// <returns></returns> 
        public List<tb_file_predelete> List(int? upid, string name, DateTime? del_start_time, DateTime? del_end_time, int? delete_id, bool isTop)
        {
            Expressionable<tb_file_predelete> queryable = Expressionable.Create<tb_file_predelete>();
            queryable.And(p => p.can_restroe == 1);
            queryable = queryable.AndIF(upid != null, p => p.upid == upid);
            queryable = queryable.AndIF(!string.IsNullOrEmpty(name), p => p.name == name);
            queryable = queryable.AndIF(del_start_time != null, p => p.delete_date >= del_start_time);
            queryable = queryable.AndIF(del_end_time != null, p => p.delete_date < del_end_time.Value.AddDays(1).Date);
            queryable = queryable.AndIF(delete_id != null, p => p.delete_id == delete_id.Value);
            queryable = queryable.AndIF(isTop, p => p.delete_id == null || p.delete_id == p.id);

            List<tb_file_predelete> result = _dal.GetList(queryable.ToExpression())
                .OrderBy(p => p.id)
                .ToList();
            return result;
        }

        #endregion

        #region 提交 

        /// <summary> 
        /// 添加 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Insert(tb_file_predelete model)
        {
            if (model.create_date == null)
            {
                model.create_date = DateTime.Now;
            }
            return _dal.Insert(model);
        }

        /// <summary> 
        /// 更新 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Update(tb_file_predelete model)
        {
            model.edit_date = DateTime.Now;
            int iCount = _dal.UpdateExcludeColumns(model, "creater", "create_date");
            return iCount == 1;
        }

        /// <summary> 
        ///  删除 
        /// </summary> 
        /// <param name="id_arr">键值</param> 
        /// <returns></returns> 
        public bool Delete(params int?[] id_arr)
        {
            return _dal.Delete(id_arr);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id">文件/文件夹ID</param>
        /// <param name="callback">callback</param>
        /// <param name="fullPath">文件/文件夹的全目录</param>
        /// <param name="type">1:文件夹 2:文件</param>
        /// <returns></returns>
        public bool DeleteFile(int id, Func<string, int, string> callback, string fullPath, int type)
        {
            _dal.BeginTran();
            try
            {
                var file = _dal.GetById(id);
                bool success = _dal.Delete(id);
                if (!success)
                {
                    throw new Exception("删除失败");
                }

                string result = callback(fullPath, type);
                if (!string.IsNullOrEmpty(result))
                {
                    throw new Exception(result);
                }

                _dal.CommitTran();
                return true;
            }
            catch (Exception)
            {
                _dal.RollbackTran();
                throw;
            }
        }

        /// <summary> 
        /// 获得要导出的数据的Excel流 
        /// </summary> 
        /// <param name="upid">上级键值</param> 
        /// <param name="name">名称</param> 
        /// <returns></returns> 
        //public byte[] Export(int? upid, string name) 
        //{
        //    List<tb_file_predelete> result = List(upid, name, null, null);
        //    return _excel.Collection2ExcelBytes(result, "Sheet1"); 
        //} 
        #endregion
    }
}
